import router from "@/routers";
import { defineStore } from "pinia";
import { TabsState, TabsMenuProps, TabsMenuItem } from "@/stores/interface";
import piniaPersistConfig from "@/config/piniaPersist";

function getsubTabsMenuList(activeMenuIdx: number, tabsMenuList: TabsMenuItem[]) {
  let subTabsMenuList: TabsMenuProps[] = [];
  tabsMenuList.some((item: TabsMenuItem) => {
    if (item.key === activeMenuIdx) {
      subTabsMenuList = item.values;
      return false;
    }
  });
  return subTabsMenuList;
}

export const useTabsStore = defineStore({
  id: "geeker-tabs",
  state: (): TabsState => ({
    tabsMenuList: [
      {
        key: 0,
        values: []
      },
      {
        key: 1,
        values: []
      },
      {
        key: 2,
        values: []
      },
      {
        key: 3,
        values: []
      },
      {
        key: 4,
        values: []
      }
    ]
  }),
  actions: {
    // Add Tabs
    async addTabs(activeMenuIdx: number, tabItem: TabsMenuProps) {
      let subTabsMenuList: TabsMenuProps[] = getsubTabsMenuList(activeMenuIdx, this.tabsMenuList);
      if (subTabsMenuList && subTabsMenuList.every(item => item.path !== tabItem.path)) {
        subTabsMenuList.push(tabItem);
        this.tabsMenuList[activeMenuIdx].values = subTabsMenuList;
      }
    },
    // Remove Tabs
    async removeTabs(activeMenuIdx: number, tabPath: string, isCurrent: boolean = true) {
      let subTabsMenuList: TabsMenuProps[] = getsubTabsMenuList(activeMenuIdx, this.tabsMenuList);
      if (subTabsMenuList) {
        if (isCurrent) {
          subTabsMenuList.forEach((item, index) => {
            if (item.path !== tabPath) return;
            const nextTab = subTabsMenuList ? subTabsMenuList[index + 1] || subTabsMenuList[index - 1] : "";
            if (!nextTab) return;
            router.push(nextTab.path);
          });
        }
        subTabsMenuList = subTabsMenuList.filter(item => item.path !== tabPath);
        this.tabsMenuList[activeMenuIdx].values = subTabsMenuList;
      }
    },
    // Close MultipleTab
    async closeMultipleTab(activeMenuIdx?: number, tabsMenuValue?: string) {
      if (activeMenuIdx !== undefined && activeMenuIdx > -1) {
        let subTabsMenuList: TabsMenuProps[] = getsubTabsMenuList(activeMenuIdx, this.tabsMenuList);
        if (subTabsMenuList) {
          subTabsMenuList = subTabsMenuList.filter(item => {
            return item.path === tabsMenuValue || !item.close;
          });
          this.tabsMenuList[activeMenuIdx].values = subTabsMenuList;
        }
      } else {
        this.tabsMenuList.forEach(item => (item.values = []));
      }
    },
    // Set Tabs
    async setTabs(activeMenuIdx: number, tabsMenuList: TabsMenuProps[]) {
      const subTabsMenuList: TabsMenuProps[] = tabsMenuList;
      this.tabsMenuList[activeMenuIdx].values = subTabsMenuList;
    },
    // Set Tabs Title
    async setTabsTitle(activeMenuIdx: number, title: string) {
      const nowFullPath = location.hash.substring(1);
      const subTabsMenuList: TabsMenuProps[] = getsubTabsMenuList(activeMenuIdx, this.tabsMenuList);
      if (subTabsMenuList) {
        subTabsMenuList.forEach(item => {
          if (item.path == nowFullPath) item.title = title;
        });
        this.tabsMenuList[activeMenuIdx].values = subTabsMenuList;
      }
    }
  },
  persist: piniaPersistConfig("geeker-tabs")
});
